/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[QuotaMails.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.util.Properties;

import javax.mail.Quota;
import javax.mail.Session;
import javax.mail.Store;

import org.junit.Before;
import org.junit.Test;

import com.sun.mail.imap.IMAPStore;

import javamail.utils.MailConstants;
import javamail.utils.PasswordUtils;

/**
 * JavaMail 设置邮件限制<br/>
 * 
 * 发送邮件[包括附件]的最大size<br/>
 * 接收邮件[包括附件]的最大size<br/>
 * 当收件人是管理员时消息的最大size<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class QuotaMails {

	private static Properties properties = null;

	private static String emailAddress = "javamail_sns@sina.com";

	@Before
	public void init() {
		properties = new Properties();
		properties.put("mail.store.protocol", MailConstants.PROTOCAL_IMAP);
		properties.put("mail.imap.host", MailConstants.IMAP_HOST_OF_SINA);
		properties.put("mail.imap.port", MailConstants.IMAP_PORT_OF_SINA);
		properties.put("mail.imap.auth", MailConstants.IS_AUTHORIZATION_NEED);
		properties.put("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));
	}

	/**
	 * 设置邮件限制[Sina邮箱不支持]
	 * 
	 * @throws Exception
	 */
	public static void quotaMails() throws Exception {
		Session session = Session.getInstance(properties);
		Store store = session.getStore(MailConstants.PROTOCAL_IMAP);
		store.connect(MailConstants.IMAP_HOST_OF_SINA, emailAddress, PasswordUtils.readPassword(emailAddress));

		IMAPStore imapStore = (IMAPStore) store;

		// 获得限制
		Quota[] quotas = imapStore.getQuota("INBOX");
		for (Quota quota : quotas) {
			System.out.println(String.format("quotaRoot:'%s'", quota.quotaRoot));
			for (Quota.Resource resource : quota.resources) {
				System.out.println(String.format("name:'%s', limit:'%s', usage:'%s'", resource.name, resource.limit,
						resource.usage));
			}
		}

		store.close();
	}

	@Test
	public void testQuotaMails() throws Exception {
		quotaMails();
	}
}
